﻿var triBeeps = [];
var flagPlayBeeps = false;
var deltaTickTimeInMills = 0;
var beepTickCounter = 0;
var beepTickLength = 0;
var currentTimeOut = null;
var currentFillTimeOut = null;
var beepProgress = null;
var tritonSong = null;
var aheadLength = 8;
var preFillStart = 0;
var forAudio = null;
var tempo = 120;
//var waitForStart=false;
//var triRunner = null;
/*
function fillStep(n) {
console.log("fillStep", n);
if (Math.random() > 0.8) {
console.log("delay");
var n = [];
for (var i = 0; i < 9999999; i++) {
n.push(beepTickCounter * beepTickCounter);
}
}
}
function fillAhead() {
if (flagPlayBeeps) {
if (preFillStart < beepTickCounter) {
console.log("fillAhead", beepTickCounter, preFillStart, "+", aheadLength);

for (var i = 0; i < aheadLength; i++) {
var n = i + aheadLength + preFillStart;
if (n >= beepTickLength) {
n = n - beepTickLength;
}
fillStep(n);
}
preFillStart = preFillStart + aheadLength;
if (preFillStart >= beepTickLength) {
preFillStart = preFillStart - beepTickLength;
}
}
window.setTimeout(fillAhead, 50);
}
}*/
function startPlayBeeps(inAudio, inTempo, length, song, progress) {
	forAudio = inAudio;
	stopPlayBeeps();
	tempo = inTempo;
	tritonSong = song;
	beepProgress = progress;
	flagPlayBeeps = true;
	id = Math.random();
	deltaTickTimeInMills = 1000 * 60.0 / 4.0 / tempo;
	beepTickCounter = 0;
	preFillStart = 0;
	beepTickLength = length;

	for (var i = 0; i < aheadLength; i++) {
		var n = i + preFillStart;
		if (n >= beepTickLength) {
			n = n - beepTickLength;
		}
		postAhead(n);
	}

	//fillAhead();
	//tickPlayBeeps();
	window.setTimeout(tickPlayBeeps, deltaTickTimeInMills * aheadLength);

	//triRunner = new Worker("js/TriRunner.js");
	//console.log(triRunner);
	/*
	triRunner.addEventListener('message', function (e) {
	console.log("from subthread", e);
	}, false);
	 */
	/*
	triRunner.postMessage({
	command : "start",
	parameter : 0
	});
	 */
	///////////////////////////////
	//var len16 = 44100 * (60.0 / 4.0) / 120;
	//var sixteenth = len16 * 32;

	//var audioBuffer = audio.audioContext.createBuffer(1, sixteenth, 44100);
	/*triRunner.postMessage({
	command : "tick",
	parameter : audioBuffer.getChannelData(0)
	});*/
	//////////////////////////////


}
function stopPlayBeeps() {
	window.clearTimeout(currentTimeOut);
	flagPlayBeeps = false;
	/*if (triRunner != null) {
	triRunner.postMessage({
	command : "stop",
	parameter : 0
	});
	triRunner = null;
	}*/
}
function removeDeafBeep() {
	var nn = beepTickCounter - 1;
	if (nn < 0) {
		nn = beepTickLength - 1;
	}
	for (var i = 0; i < triBeeps.length; i++) {
		if (triBeeps[i].step == nn) {
			triBeeps.splice(i, 1);
			return true;
		}
	}
	return false;
}
function postAhead(n) {
	for (var i = 0; i < tritonSong.slots.length; i++) {
		var songSlot = tritonSong.slots[i];
		for (var r = 0; r < songSlot.channels.length; r++) {
			var riffChannel = songSlot.channels[r];
			for (var k = 0; k < riffChannel.notes.length; k++) {
				var channelNote = riffChannel.notes[k];
				var step = channelNote.step + songSlot.step;
				if (n == step) {
					forAudio.renderNote(step, riffChannel.instrument //
					, tempo, channelNote.length, channelNote.pitch, channelNote.glissando);
				}
			}
			for (var k = 0; k < riffChannel.kicks.length; k++) {
				var channelKick = riffChannel.kicks[k];
				var step = riffChannel.kicks[k].step + songSlot.step;
				//console.log(step);
				if (n == step) {
					//forAudio.renderKick(step, riffChannel.kicks[k].instrument, tempo);
					forAudio.renderNote(step, riffChannel.kicks[k].instrument //
					, tempo,8, 40, 40);
				}
			}
		}
	}
}
function doTick() {
	for (var i = 0; i < triBeeps.length; i++) {
		if (triBeeps[i].step == beepTickCounter) {
			triBeeps[i].beep();
		}
	}
	while (removeDeafBeep()) {
		//
	}
	var n = beepTickCounter + aheadLength;
	if (n >= beepTickLength) {
		n = n - beepTickLength;
	}
	postAhead(n);
}
function tickPlayBeeps() {
	if (flagPlayBeeps) {

		var startTime = new Date().getTime();
		doTick();
		beepProgress(beepTickCounter);
		beepTickCounter++;
		if (beepTickCounter >= beepTickLength) {
			beepTickCounter = 0;
		}
		var spentTime = new Date().getTime() - startTime;
		var correctedDelta = deltaTickTimeInMills - spentTime;
		if (correctedDelta < 20) {
			correctedDelta = 20;
		}
		currentTimeOut = window.setTimeout(tickPlayBeeps, correctedDelta);
	}
}
